package org.dromara.third.convert.enterprise.validator;

import cn.dev33.satoken.annotation.SaIgnore;
import org.dromara.common.core.service.DeptService;
import org.dromara.common.core.service.UserService;
import org.dromara.common.core.utils.ValidateUtil;
import org.dromara.third.auth.beans.constans.ExceptionEnum;
import org.dromara.third.convert.enterprise.dto.EnterpriseFullDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import java.util.List;

@Component
@SaIgnore
public class EnterpriseValidator implements Validator {

    @Autowired
    private DeptService deptService;

    @Autowired
    private UserService userService;

    @Override
    public boolean supports(Class<?> clazz) {
        return  List.class.isAssignableFrom(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        List<EnterpriseFullDTO> enterpriseFullDTO = (List<EnterpriseFullDTO>) target;

        // 基础字段验证
        validateRequiredFields(enterpriseFullDTO, errors);
    }

    /**
     * 验证必填字段
     */
    private void validateRequiredFields(List<EnterpriseFullDTO> dtos, Errors errors) {
        dtos.forEach(dto -> {
            ValidateUtil.rejectIfNull(dto::getId, String.valueOf(ExceptionEnum.AuthException01.getCode()), "主键不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getQymc, String.valueOf(ExceptionEnum.AuthException01.getCode()), "企业名称不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getQyjc, String.valueOf(ExceptionEnum.AuthException01.getCode()), "企业简称不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getYshxydm, String.valueOf(ExceptionEnum.AuthException01.getCode()), "统一社会信用代码不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getFrmc, String.valueOf(ExceptionEnum.AuthException01.getCode()), "法定代表人不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getFrlxfs, String.valueOf(ExceptionEnum.AuthException01.getCode()), "法定代表人联系方式不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getAddr, String.valueOf(ExceptionEnum.AuthException01.getCode()), "地址不能为空", errors);
            dto.getDqyhj().forEach(dqyhj -> {
                ValidateUtil.rejectIfEmpty(dqyhj::getDqyhjDbx, String.valueOf(ExceptionEnum.AuthException01.getCode()), "点位对象类型不能为空", errors);
                ValidateUtil.rejectIfEmpty(dqyhj::getDqyhjWtk, String.valueOf(ExceptionEnum.AuthException01.getCode()), "点位对象坐标类型不能为空", errors);
                ValidateUtil.rejectIfEmpty(dqyhj::getDqyhjQymc, String.valueOf(ExceptionEnum.AuthException01.getCode()), "区域名称不能为空", errors);
            });
            ValidateUtil.rejectIfEmpty(dto::getDelFlag, String.valueOf(ExceptionEnum.AuthException01.getCode()), "删除标志不能为空", errors);
            ValidateUtil.rejectIfNull(dto::getCreateTime, String.valueOf(ExceptionEnum.AuthException01.getCode()), "创建时间不能为空", errors);
            ValidateUtil.rejectIfNull(dto::getUpdateTime, String.valueOf(ExceptionEnum.AuthException01.getCode()), "更新时间不能为空", errors);
            ValidateUtil.rejectIfNull(dto::getCreateBy, String.valueOf(ExceptionEnum.AuthException01.getCode()), "创建人不能为空", errors);
            ValidateUtil.rejectIfNull(dto::getUpdateBy, String.valueOf(ExceptionEnum.AuthException01.getCode()), "更新人不能为空", errors);
        });
    }
}
